package com.wing.api.query.cabinet.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wing.api.ApplicationContextUtil;
import com.wing.api.query.cabinet.BaseQueryCabinetService;
import com.wing.cabinet.model.entity.Cabinet;
import com.wing.cabinet.model.response.EnterpricceIdVO;
import com.wing.cabinet.service.CabinetService;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.JsonResult;
import com.wing.enterprise.feign.EnterpriseClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 刀柜查询接口
 */
@Service
@Slf4j
public class StandardQueryCabinetServiceImpl implements BaseQueryCabinetService {

    @Override
    public JsonResult<List<Cabinet>> queryList(JSONObject jsonObject, EnterpricceIdVO enterpricceIdVo) {
        log.info("请求参数,{}", jsonObject.toJSONString());
        //此处需要做接口签名验证
        //sign = md5（参数1+参数2+参数3+key）
        String sourceSign = jsonObject.getString("sign"); // 签名值

        if(StringUtils.isBlank(sourceSign)){
            throw new BusinessException("签名值为空！");
        }

        StringBuffer tempStr = new StringBuffer();
        tempStr.append(enterpricceIdVo.getKey());
        log.info("签名源串,{}", sourceSign);
        String desSign = DigestUtil.md5Hex(tempStr.toString());
        log.info("签名目的串,{}", desSign);
        if(!desSign.equals(sourceSign)){
            throw new BusinessException("签名不通过！");
        }

        CabinetService cabinetService = (CabinetService) ApplicationContextUtil.getBean("cabinetServiceImpl");
        List<Cabinet> list = cabinetService.list(new QueryWrapper<Cabinet>().eq("cabinet_nature", Cabinet.CabinetNature.main.getValue()));
        return JsonResult.success(list);

    }

    @Override
    public JsonResult queryDetail(Long id, EnterpricceIdVO enterpricceIdVo, String sign) {
        log.info("请求参数,id="+id+",sign="+sign);
        //此处需要做接口签名验证
        //sign = md5（参数1+参数2+参数3+key）

        if(StringUtils.isBlank(sign)){
            throw new BusinessException("签名值为空！");
        }

        StringBuffer tempStr = new StringBuffer();
        tempStr.append(id);
        tempStr.append(enterpricceIdVo.getKey());
        log.info("签名源串,{}", tempStr.toString());
        String desSign = DigestUtil.md5Hex(tempStr.toString());
        log.info("签名目的串,{}", desSign);
        if(!desSign.equals(sign)){
            throw new BusinessException("签名不通过！");
        }
        EnterpriseClient enterpriseClient = (EnterpriseClient) ApplicationContextUtil.getBean("enterpriseClientFallback");

        return enterpriseClient.foreignProductInfoDetail(id);
    }

    public static void main(String[] args) {
//        String key = "e10adc3949ba59abbe56e057f20f883e";
        String key = "$2a$10$fxpgo/SLFHCeNThnfF/q7uvoZJOxbJBNNI4.dshzlntgBIHKI9eb.";
        StringBuffer tempStr = new StringBuffer();
        tempStr.append(key);
//        log.info("签名源串,{}", sourceSign);
        String desSign = DigestUtil.md5Hex(tempStr.toString());
        log.info("签名目的串,{}", desSign);
//        if(!desSign.equals(sourceSign)){
//            throw new BusinessException("签名不通过！");
//        }
        System.out.println(desSign);

    }

}
